public class test5 {
    class Solution {
        public String longestPalindrome(String s) {
            int len = s.length();
            if (len < 2)
                return s;//如果为空或者只有一个则直接返回

            int maxlen = 1;
            int begin = 0;
            boolean[][] dp = new boolean[len][len];
            char[] charS = s.toCharArray();
            for (int i = 1; i <= len; i++) {
                dp[i - 1][i - 1] = true;
//            if(charS[i] == charS[i-1]){
//                dp[i][i+1] = true;
//            }else{
//                dp[i][i+1] = false;
//            }
            }
            for (int L = 2; L <= len; L++) {
                for (int i = 0; i < len; i++) {
                    int j = L + i - 1;
                    if (j >= len) {
                        break;
                    }
                    if (charS[i] == charS[j]) {
                        if (j - i < 3) {
                            dp[i][j] = true;
                        } else {
                            dp[i][j] = dp[i + 1][j - 1];
                        }
                    } else {
                        dp[i][j] = false;
                    }
                    if (dp[i][j] && j - i + 1 > maxlen) {
                        maxlen = j - i + 1;
                        begin = i;
                    }
                }
            }
            return s.substring(begin, begin + maxlen);
        }
    }
}